//
//  Lk.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/9.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

enum LDireccion {
    case C2L
    case C2R
    case C2T
    case C2B
    case C2C
    case T2L
    case T2R
    case B2L
    case B2R
}

class Lk: NSObject {
    
    static let shared = Lk()
    var contentViewController:NSViewController?
    var isHide:Bool = true
    
    var current:DSAppearance = .dark
    
    static func create(controller:NSViewController,current:DSAppearance = .dark) -> Self {
        let lk = Lk.shared
        lk.current = current
        lk.contentViewController = controller
        return lk as! Self
    }
    
    func show(relativeTo positioningPoint: NSPoint, of positioningView: NSView, direccion: LDireccion = .C2B,dismissBlock:os_block_t?=nil) {
        guard let controller = self.contentViewController  else {
            return
        }
        controller.view.wantsLayer = true
        controller.view.layer?.masksToBounds = true
        controller.view.layer?.cornerRadius = 1
        
        Dialog.shared.setup(content: controller.view)
        Dialog.shared.current = self.current
        Dialog.shared.dismissBlock = dismissBlock
        Dialog.shared.show(relativeTo: nil)
        
        self.isHide = false
        
        Dialog.shared.bgContainer.snp.remakeConstraints { (make) in
            make.size.equalTo(controller.view.bounds.size)
            switch direccion {
            case .T2L:
                make.left.equalTo(positioningView).offset(positioningPoint.x)
                make.bottom.equalTo(positioningView.snp.top).offset(positioningPoint.y)
            case .T2R:
                make.right.equalTo(positioningView).offset(positioningPoint.x)
                make.bottom.equalTo(positioningView.snp.top).offset(positioningPoint.y)
            case .B2L:
                make.left.equalTo(positioningView).offset(positioningPoint.x)
                make.top.equalTo(positioningView.snp.bottom).offset(positioningPoint.y)
            case .B2R:
                make.right.equalTo(positioningView).offset(positioningPoint.x)
                make.top.equalTo(positioningView.snp.bottom).offset(positioningPoint.y)
            case .C2L:
                make.centerY.equalTo(positioningView).offset(positioningPoint.y)
                make.right.equalTo(positioningView.snp.left).offset(positioningPoint.x)
            case .C2R:
                make.centerY.equalTo(positioningView).offset(positioningPoint.y)
                make.left.equalTo(positioningView.snp.right).offset(positioningPoint.x)
            case .C2T:
                make.centerX.equalTo(positioningView).offset(positioningPoint.x)
                make.bottom.equalTo(positioningView.snp.top).offset(positioningPoint.y)
            case .C2B:
                make.centerX.equalTo(positioningView).offset(positioningPoint.x)
                make.top.equalTo(positioningView.snp.bottom).offset(positioningPoint.y)
            case .C2C:
                make.centerX.equalTo(positioningView).offset(positioningPoint.x)
                make.centerY.equalTo(positioningView).offset(positioningPoint.y)
            }
        }
    }
    
    func show() {
        guard let controller = self.contentViewController  else {
            return
        }
        Dialog.shared.setup(content: controller.view)
        Dialog.shared.show(relativeTo: nil)
        self.isHide = false
        
        Dialog.shared.bgContainer.snp.remakeConstraints { (make) in
            make.size.equalTo(controller.view.bounds.size)
            make.center.equalToSuperview()
        }
    }
    
    func dismiss() {
        Dialog.shared.dismiss()
        self.isHide = true
        self.contentViewController = nil
    }
    
}

extension NSView {
    
    func addAnimation(startFrame:NSRect,endFrame:NSRect,duration:Double,delegate:NSAnimationDelegate?) {
        let dict:[[NSViewAnimation.Key : Any]] = [
            [NSViewAnimation.Key.target:self],
            [NSViewAnimation.Key.effect:NSViewAnimation.EffectName.fadeIn],
            [NSViewAnimation.Key.startFrame:startFrame],
            [NSViewAnimation.Key.endFrame:endFrame]
        ]
        let animation = NSViewAnimation.init(viewAnimations: dict)
        animation.duration = duration
        animation.delegate = delegate
        //NSAnimationBlocking阻塞
        //NSAnimationNonblocking异步不阻塞
        //NSAnimationNonblockingThreaded线程不阻塞
        animation.animationBlockingMode = .nonblockingThreaded
        animation.start()
    }
    
    
    func addAnimations()   {
        NSAnimationContext.runAnimationGroup({ (context) in
            context.duration = 2
            //let timingFunc = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeOut)
            //context.timingFunction = timingFunc
            self.animator().alphaValue = 0.4
        }) {
            NSAnimationContext.runAnimationGroup({ (context) in
                context.duration = 2
                self.animator().alphaValue = 1.0
            }) {
                
            }
        }
    }
}
